/***********************************************************************
 * Dao Interface :  SysMenuResourceDao.java
 * Author:  Bill Vison
 * Purpose: Defines the Hibernate Implement SysMenuResourceDaoImpl
 ***********************************************************************/
package org.vsg.security.model.persistence.hibernate;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.PaginationException;
import org.vsg.common.pagination.dao.PaginationHibernateDaoSupport;
import org.vsg.common.pagination.datatrans.PaginationProvider;
import org.vsg.security.model.domain.SysMenuResource;
import org.vsg.security.model.persistence.SysMenuResourceDao;
import org.vsg.security.model.persistence.hibernate.proc.SysMenuResourceByAllPaginationProc;
import org.vsg.security.model.persistence.hibernate.proc.SysMenuResourcePaginationProc;

/**
 * @author Bill Vison
 */
public class SysMenuResourceDaoImpl extends PaginationHibernateDaoSupport implements SysMenuResourceDao {

	/**
	 * set the logger for develope code , do not use System.print(ln) to out print message.
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysMenuResourceDaoImpl.class);

	
	/**
	 *
	 * @param sysMenuResource
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysMenuResourceDao#save(org.vsg.security.model.domain.SysMenuResource)
	 */
	@Override
	public void save(SysMenuResource sysMenuResource) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(sysMenuResource);
	}

	@Override
	public void saveAll(Collection<SysMenuResource> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		
		getHibernateTemplate().saveOrUpdateAll(all);
		
	}   	
   	
	/**
	 *
	 * @param sysMenuResource
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysMenuResourceDao#update(org.vsg.security.model.domain.SysMenuResource)
	 */
   	@Override
   	public void update(SysMenuResource sysMenuResource) throws DataAccessException {
   		// TODO Auto-generated method stub
		getHibernateTemplate().update(sysMenuResource);
	}   	


	/**
	 *
	 * @param sysMenuResource
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysMenuResourceDao#delete(org.vsg.security.model.domain.SysMenuResource)
	 */
	@Override
	public void delete(SysMenuResource sysMenuResource) throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(sysMenuResource);
	}

	@Override
	public void deleteAll(Collection<SysMenuResource> all)
			throws DataAccessException {
		// TODO Auto-generated method stub
		getHibernateTemplate().deleteAll(all);
	}
   	
   	
	/**
	 *
	 * @param pk , the primary key
	 * @exception DataAccessException
	 * @see org.vsg.security.model.persistence.SysMenuResourceDao#load(java.io.Serializable)
	 */
	@Override
	public SysMenuResource load(Serializable pk) throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		SysMenuResource sysMenuResource = (SysMenuResource) getHibernateTemplate().load(SysMenuResource.class, pk);
		
		return sysMenuResource;	
		// End of user code	
	}
   
   
	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysMenuResourceDao#pageSysMenuResource(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysMenuResource(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysMenuResourcePaginationProc proc = new SysMenuResourcePaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code	
	}      	


	/**
	 *
	 * @param provider
	 * @exception DataAccessException
	 * @exception PaginationException
	 * @see org.vsg.security.model.persistence.SysMenuResourceDao#pageSysMenuResourceByAll(org.vsg.common.pagination.datatrans.PaginationProvider)
	 */
	@Override
	public Pagination pageSysMenuResourceByAll(PaginationProvider provider) throws DataAccessException, PaginationException {
		// Start of user code TODO Auto-generated method stub
		SysMenuResourceByAllPaginationProc proc = new SysMenuResourceByAllPaginationProc();
		
		Pagination page = generatePagination(proc , provider);
		
		return page;
		// End of user code		
	}


	/**
	 * @exception DataAccessException
	 */
	@Override
	public Collection<SysMenuResource> findByAll() throws DataAccessException {
		// Start of user code TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");

		query.append("sysMenuResource ");
		query.append("from ").append(SysMenuResource.class.getName()).append(" as sysMenuResource ");
		query.append("where sysMenuResource.flagDeleted = 0 ");
		query.append("order by sysMenuResource.name");
		
		Collection params = new Vector();
		
		logger.info("query : " + query.toString());

		Iterator<SysMenuResource> iter = getHibernateTemplate().iterate(query.toString());
		
		Collection<SysMenuResource> result = new Vector<SysMenuResource>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
		// End of user code			
	}

	@Override
	public Collection<SysMenuResource> findRootMenuResource()
			throws DataAccessException {
		StringBuilder query = new StringBuilder("select ");
		query.append("sysMenuResource ");
		query.append("from ").append(SysMenuResource.class.getName()).append(" as sysMenuResource ");
		query.append("where sysMenuResource.parentId = 0 ");
		
		Iterator<SysMenuResource> iter = this.getHibernateTemplate().iterate(query.toString());
		
		Collection<SysMenuResource> results = new Vector<SysMenuResource>();

		while (iter.hasNext()) {
			results.add( iter.next() );
		}
		
		return results;
	}

	
	
	@Override
	public Set<SysMenuResource> findAvailableRoot() throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuilder query = new StringBuilder("select ");
		query.append("sysMenuResource ");
		query.append("from ").append(SysMenuResource.class.getName()).append(" as sysMenuResource ");
		query.append("where sysMenuResource.flagDeleted = 0 and sysMenuResource.flagDisabled = 0 and sysMenuResource.parentId = 0 ");
		
		Iterator<SysMenuResource> iter = this.getHibernateTemplate().iterate(query.toString());
		
		Set<SysMenuResource> results = new HashSet<SysMenuResource>();

		while (iter.hasNext()) {
			results.add( iter.next() );
		}
		
		return results;
	}

	@Override
	public Collection<SysMenuResource> findChildrenResource(
			SysMenuResource resource) throws DataAccessException {
		// TODO Auto-generated method stub
		StringBuffer query = new StringBuffer("select ");
		query.append("sysMenuResource ");
		query.append("from ").append(SysMenuResource.class.getName()).append(" as sysMenuResource ");
		query.append("where sysMenuResource.parentId = ?");
		
		Collection params = new Vector();
		params.add(resource.getId());
		
		Iterator<SysMenuResource> iter = this.getHibernateTemplate().iterate(query.toString() , params.toArray());
		
		Collection<SysMenuResource> result = new Vector<SysMenuResource>();
		
		while (iter.hasNext()) {
			result.add( iter.next() );
		}
		
		return result;
	}

	
	
   
} 